package seminar

/*研究班、自定义\自定义layer*/

import (
	"fmt"

	"github.com/google/gopacket"
)

/*特许经营权*/
type Franchise struct {
	SomeByte    byte
	AnotherByte byte
	restofData  []byte
}

/*
第一个参数为id:(id<0 || id>2000)
*/
var SeminarLayerType = gopacket.RegisterLayerType(2001, gopacket.LayerTypeMetadata{Name: "SeminerType", Decoder: gopacket.DecodeFunc(decodeFranchiseLayer)})

func (s Franchise) LayerType() gopacket.LayerType { return SeminarLayerType }
func (s Franchise) LayerContents() []byte         { return []byte{s.SomeByte, s.AnotherByte} }
func (s Franchise) LayerPayload() []byte          { return s.restofData }

// 难以理解的
func Intangible() {
	// 暂存
	provisional := []byte{0xF0, 0x0F, 65, 65, 66, 67, 68}
	packet := gopacket.NewPacket(provisional, SeminarLayerType, gopacket.Default)
	fmt.Println(packet)
	//投资组合、公事包
	portfolio := packet.Layer(SeminarLayerType)
	if portfolio != nil {
		//拍卖
		auction, _ := portfolio.(*Franchise)
		//结构
		fmt.Printf("payload:%s,someByte composition:%x,AnotherByte composition:%x\n", auction.LayerPayload(), auction.SomeByte, auction.AnotherByte)
	}
}

func decodeFranchiseLayer(data []byte, p gopacket.PacketBuilder) error {
	p.AddLayer(&Franchise{data[0], data[1], data[2:]})
	return p.NextDecoder(gopacket.LayerTypePayload)
}
